package cn.huapu.power.energy.service.impl;

import cn.huapu.power.energy.bean.PeaFeeoptimize;
import cn.huapu.power.energy.bean.bo.DateParameterBo;
import cn.huapu.power.energy.mapper.PeaFeeoptimizeMapper;
import cn.huapu.power.energy.service.PeaFeeoptimizeService;
import cn.huapu.power.energy.util.ObjectHelper;
import cn.huapu.power.energy.util.ValidateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 测试用户权限服务实现类
 * @author autogen
 *
 */
@Service
public class PeaFeeoptimizeServiceImpl extends BaseServiceImpl<PeaFeeoptimizeMapper, PeaFeeoptimize,Integer> implements PeaFeeoptimizeService {
    @Autowired
    private PeaFeeoptimizeMapper zeMapper;

    @Override
    @Transactional(readOnly = true)
    public Map<String,Object> pricePattern(DateParameterBo da) {
        //获取数据库最近一次时间
        PeaFeeoptimize pea = zeMapper.getDateLately(da);
        if(pea != null) {
	        //当月
	        da.setTable("pea_feeoptimize");
	        da.setYear(pea.getYear()+"");
	        da.setMonth(pea.getMonth()+"");
	        List<PeaFeeoptimize> list = zeMapper.pricePattern(da);
	        Map<String, String> map = getcyDate(pea.getYear(), pea.getMonth());
	
	        //次月
	        da.setTable("pea_powerforcast");
	        da.setYear(map.get("year"));
	        da.setMonth(map.get("month"));
	        List<PeaFeeoptimize> list1 = zeMapper.pricePattern(da);
	        Map<String,Object> map1 = new LinkedHashMap<>();
	        map1.put("dyFees",list);
	        map1.put("cyFees",list1);
	        return map1;
        }
        return new LinkedHashMap<>();
    }

    @Override
    @Transactional(readOnly = true)
    public List<PeaFeeoptimize> bycyPower(DateParameterBo da) {
        //获取数据库最近一次时间
        PeaFeeoptimize pea = zeMapper.getDateLately(da);
        //当月
        da.setTable("pea_feeoptimize");
        da.setYear(pea.getYear()+"");
        da.setMonth(pea.getMonth()+"");
        PeaFeeoptimize peaFeeoptimize = zeMapper.bycyPower(da);
        Map<String, String> map = getcyDate(pea.getYear(), pea.getMonth());
        //次月
        da.setTable("pea_powerforcast");
        da.setYear(map.get("year"));
        da.setMonth(map.get("month"));
        PeaFeeoptimize peaFeeoptimize1 = zeMapper.bycyPower(da);
        List<PeaFeeoptimize> list = new ArrayList<>();
        list.add(peaFeeoptimize);
        list.add(peaFeeoptimize1);
        return list;
    }

   // @Cacheable(cacheNames = "platcache", key = "'xftgBaseInfo_'+#da.id")
    @Override
    @Transactional(readOnly = true)
    public Map<String,Object> xftgBaseInfo(DateParameterBo da) {
        Map<String,Object> map = new LinkedHashMap<>();
        //销峰填谷功率因数信息
        da.setType2("实际峰谷");
        PeaFeeoptimize tzqpower =  zeMapper.xftgBaseInfo(da);
        da.setType2("调整后峰谷");
        PeaFeeoptimize tzhpower =  zeMapper.xftgBaseInfo(da);


        //各项对比图
        da.setType2("实际峰谷");
        PeaFeeoptimize sjfgfee = zeMapper.gxdbChart(da);
        da.setType2("调整后峰谷");
        PeaFeeoptimize tzhfgfee = zeMapper.gxdbChart(da);
        //最大值
        if(tzhfgfee != null) {
        	da.setYear(tzhfgfee.getYear()+"");
        	da.setMonth(tzhfgfee.getMonth()+"");
        }
        else {
        	LocalDate now = LocalDate.now().minusMonths(1);//上月
        	da.setYear(now.getYear()+"");
        	da.setMonth(now.getMonthValue()+"");
        }
        List<PeaFeeoptimize> maxfee = zeMapper.getMax(da);
        PeaFeeoptimize pea = new PeaFeeoptimize();
        if(!ValidateUtil.listIsNull(maxfee)){
            for (int i = 0; i < maxfee.size(); i++) {
                if((i+1)<maxfee.size()){
                    if(maxfee.get(i).getJianfee()>maxfee.get(i+1).getJianfee()){
                        pea.setJianfee(maxfee.get(i).getJianfee());
                    }else{
                        pea.setJianfee(maxfee.get(i+1).getJianfee());
                    }
                    if(maxfee.get(i).getFengfee()>maxfee.get(i+1).getFengfee()){
                        pea.setFengfee(maxfee.get(i).getFengfee());
                    }else{
                        pea.setFengfee(maxfee.get(i+1).getFengfee());
                    }
                    if(maxfee.get(i).getPingfee()>maxfee.get(i+1).getPingfee()){
                        pea.setPingfee(maxfee.get(i).getPingfee());
                    }else{
                        pea.setPingfee(maxfee.get(i+1).getPingfee());
                    }
                    if(maxfee.get(i).getGufee()>maxfee.get(i+1).getGufee()){
                        pea.setGufee(maxfee.get(i).getGufee());
                    }else{
                        pea.setGufee(maxfee.get(i+1).getGufee());
                    }
                    if(maxfee.get(i).getMulufee()>maxfee.get(i+1).getMulufee()){
                        pea.setMulufee(maxfee.get(i).getMulufee());
                    }else{
                        pea.setMulufee(maxfee.get(i+1).getMulufee());
                    }
                    if(maxfee.get(i).getAvgfee()>maxfee.get(i+1).getAvgfee()){
                        pea.setAvgfee(maxfee.get(i).getAvgfee());
                    }else{
                        pea.setAvgfee(maxfee.get(i+1).getAvgfee());
                    }

                }

            }
        }

        map.put("tzqpower",tzqpower);
        map.put("tzhpower",tzhpower);
        map.put("sjfgfee",sjfgfee);
        map.put("tzhfgfee",tzhfgfee);
        map.put("maxfee",pea);
        return map;
    }

    @Override
    @Transactional(readOnly = true)
    public PeaFeeoptimize queryMinFee(DateParameterBo da) {
        PeaFeeoptimize pea = zeMapper.queryMinFee(da);
        return pea;
    }
    /**
     * @Author jzx
     * @Description //根据当前时间获取次月时间
     * @Date 16:27 2019-7-19
     * @Param [time]
     * @return java.util.Map<java.lang.String,java.lang.String>
     **/
    public Map<String,String> getcyDate(Integer year,Integer month) {        
        int year1 = year;
        int month1 = month;
        if(month1 ==12){
            year1 += 1;
            month1 = 1;
        }else{           
            month1+=1;
        }
        Map<String,String> map = new HashMap<String,String>(2);
        map.put("year",String.valueOf(year1));
        map.put("month",String.valueOf(month1));
       
        return map;
    }
}
